import bot from "./bot.js"
import onMessage from "../bussiness/message.js"
import {onRoomTopic,onRoomJoin,onRoomLeave} from "../bussiness/room.js"
import rcodeTerminal from "qrcode-terminal";
import { config } from '../config/index.js';
import initDay from "../scheduleTask/index.js"
// 二维码生成
function onScan(qrcode, status) {
  rcodeTerminal.generate(qrcode); // 在console端显示二维码
  const qrcodeImageUrl = [
    'https://api.qrserver.com/v1/create-qr-code/?data=',
    encodeURIComponent(qrcode),
  ].join('');

  console.log(qrcodeImageUrl);
}

// 登录
async function onLogin(user) {
  console.log(`贴心小助理${user}登录了`);
  const date = new Date()
  console.log(`当前容器时间:${date}`);
  if (config.AUTOREPLY) {
    console.log(`已开启机器人自动聊天模式`);
  }
  // 登陆后创建定时任务
  // await initDay();
}

// 登出
function onLogout(user) {
  console.log(`小助手${user} 已经登出`);
}

async function onReady () {
await initDay()
 
}

bot.on('ready', onReady)
  bot.on('scan', onScan);
  bot.on('login', onLogin);
  bot.on('logout', onLogout);
  bot.on('message', onMessage);
  bot.on('room-topic', onRoomTopic);
  bot.on('room-join', onRoomJoin,{});
  bot.on('room-leave', onRoomLeave);
  bot.on('room-invite', onRoomJoin,{});


  
  // console.log(WechatyWeixinOpenAI)
  bot
    .start()
    .then(() => console.log('开始登陆微信'))
    .catch((e) => {
      console.error(e)
    });
